package com.sanpu.kxe.business.repay.view;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.sanpu.kxe.R;
import com.sanpu.kxe.base.BaseTitleActivity;
import com.sanpu.kxe.business.main.provider.IMainProvider;
import com.sanpu.kxe.business.repay.adapter.RepayCreditAdapter;
import com.sanpu.kxe.business.repay.bean.RepayCreditOutput;
import com.sanpu.kxe.business.repay.contract.RepayConfig;
import com.sanpu.kxe.business.repay.contract.RepayCreditContract;
import com.sanpu.kxe.business.repay.presenter.RepayCreditPresenter;
import com.sanpu.kxe.business.trade.bean.PayCollectionOutput;
import com.sanpu.kxe.common.interfaces.CommonConfig;
import com.sanpu.kxe.common.interfaces.UrlConstants;
import com.sanpu.kxe.jump.eventbus.RefreshBankEvent;
import com.sanpu.kxe.module.immersionbar.ImmersionBar;
import com.sanpu.kxe.utils.PublicProviderUtil;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Description：代还首页
 * Created by 薛勇军 on 2018/8/6.
 */

public class RepayCreditActivity extends BaseTitleActivity implements RepayCreditContract.View {

    @Bind(R.id.creditLv)
    ListView creditLv;

    private TextView payRateTv;
    private TextView repayRateTv;
    private ImageView payRateBtn;
    private LinearLayout addCreditLl;

    private RepayCreditAdapter mAdapter;

    private RepayCreditContract.Presenter mPresenter;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_repay_credit;
    }

    @Override
    protected void initView() {
        super.initView();
        mPresenter = new RepayCreditPresenter(this);
        PayCollectionOutput collectionOutput = (PayCollectionOutput) getIntent().getSerializableExtra(RepayConfig.INTENT_PAYCOLLECTION);
        mPresenter.initViewData(collectionOutput);

        View headView = LayoutInflater.from(mContext).inflate(R.layout.view_repay_credit_head, null);
        payRateTv = ButterKnife.findById(headView, R.id.payRateTv);
        repayRateTv = ButterKnife.findById(headView, R.id.repayRateTv);
        payRateBtn = ButterKnife.findById(headView, R.id.payRateBtn);
        creditLv.addHeaderView(headView);

        View footerView = LayoutInflater.from(mContext).inflate(R.layout.view_repay_credit_footer, null);
        addCreditLl = ButterKnife.findById(footerView, R.id.addCreditLl);
        if (collectionOutput != null && !TextUtils.equals("DISABLE", collectionOutput.getEnableStatus())) {
            creditLv.addFooterView(footerView);
        }

        mAdapter = new RepayCreditAdapter(new ArrayList<RepayCreditOutput.RepayCredit>(),
                mContext, mPresenter.getRepayCreditListener());
        creditLv.setAdapter(mAdapter);
    }

    @Override
    protected void setListener() {
        super.setListener();
        addCreditLl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.addCreditCard();
            }
        });
        payRateBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                RepayOpenProvider.getInstance().openRateDetailsActivity(mContext);
                mPresenter.openRepaySupportActivity();
            }
        });
        creditLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position > 0 && position <= mAdapter.getCount()) {
                    RepayCreditOutput.RepayCredit repayCredit = mAdapter.getItem(position - 1);
                    if (repayCredit != null) {
//                        RepayOpenProvider.getInstance().openUpdateCreditActivity(
//                                mContext, repayCredit, CommonConfig.REQUESTCODE_REPAY_CREDIT);
                    }
                }
            }
        });
    }

    @OnClick({R.id.leftIv, R.id.help})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.leftIv:
                finish();
                break;
            case R.id.help:
                IMainProvider provider = PublicProviderUtil.getProvider(IMainProvider.class);
                if (provider != null) {
                    provider.openBrowserX5Activity(mContext, UrlConstants.REPAY_HELP, true);
                }
                break;
        }
    }

    @Override
    public void showDataView(RepayCreditOutput creditOutput) {
        if (creditOutput != null) {
            if (!TextUtils.isEmpty(creditOutput.getTradeRate())) {
                payRateTv.setText("" + creditOutput.getTradeRate() + "%");
            }
            if (!TextUtils.isEmpty(creditOutput.getExtraCharge())) {
                repayRateTv.setText("" + creditOutput.getExtraCharge() + "元/笔");
            }
            mAdapter.setNotifyDataChanged(creditOutput.getCardList());
        }
    }

    /**
     * 银行卡变更，重新请求
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(RefreshBankEvent event) {
        if (event != null) {
            mPresenter.index();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == CommonConfig.REQUESTCODE_REPAY_CREDIT) {
            mPresenter.index();
        }
    }

    @Override
    protected void initImmersionBar() {
        mImmersionBar = ImmersionBar.with(this);
        mImmersionBar.statusBarColorTransformEnable(true)
                .statusBarView(R.id.topView)
                .statusBarColor(R.color.color_base)
                .keyboardEnable(true)
                .navigationBarWithKitkatEnable(false)
                .init();
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    protected boolean hasCommonTitle() {
        return false;
    }

    @Override
    public void setPresenter(RepayCreditContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
